其他
每个新手程序员都应该知道的Python开发技巧
The following article is from 萝卜大杂烩 Author Peter Nistrup
翻译 | 周萝卜
出品 | 萝卜大杂烩(ID:luobodazahui)
字符串操作
列表推导
Lambda 和 Map 函数
在一行里使用 if elif 和 else 条件判断
zip() 函数
>>> print(my_string * 2)
Hi Medium..!Hi Medium..!
>>> print(my_string + " I love Python" * 2)
Hi Medium..! I love Python I love Python
!..muideM iH
>>> my_list = [1,2,3,4,5]
>>> print(my_list[::-1])
[5, 4, 3, 2, 1]
>>> print(' '.join(word_list[::-1]) + '!')
this is awesome!
>>> return x**2 + 5
>>> new_list = []
>>> for x in my_list:
>>> if x % 2 != 0:
>>> new_list.append(stupid_func(x))
>>> print(new_list)
[6, 14, 30]
>>> print([stupid_func(x) for x in my_list if x % 2 != 0])
[6, 14, 30]
>>> if conditional:
>>> expression
[6, 14, 30]
>>> print([stupid_func(1), stupid_func(3), stupid_func(5)])
[6, 14, 30]
>>> print(sorted(my_list))
[-2, -1, 0, 1, 2]
[0, -1, 1, -2, 2]
[4, 10, 18]
>>> z = []
>>> for i in range(len(x)):
>>> z.append(x[i] * y[i])
>>> print(z)
[4, 10, 18]
>>> if x >= 10:
>>> print("Horse")
>>> elif 1 < x < 10:
>>> print("Duck")
>>> else:
>>> print("Baguette")
>>> last_names = ["Jensen", "Smith", "Nistrup"]
>>> print([' '.join(x) for x in zip(first_names, last_names)])
['Peter Jensen', 'Christian Smith', 'Klaus Nistrup']
['Peter Nistrup', 'Christian Smith', 'Klaus Jensen']
https://towardsdatascience.com/python-tricks-101-what-every-new-programmer-should-know-c512a9787022
◆
精彩推荐
◆
5大必知的图算法,附Python代码实现
如何用爬虫技术帮助孩子秒到心仪的幼儿园(基础篇)
Python传奇:30年崛起之路